package com.wz.datachannel.util;

import java.util.List;

import com.alibaba.fastjson.JSON;

import android.text.TextUtils;

/**
 * json解析工具类{fastJson}
 * <p>
 * Json->javaBean
 * 
 * @author LW
 *
 */
public class FastJsonUtil {

	/**
	 * Json->javaBean
	 * 
	 * @param jsonString
	 * @param cls
	 * @return
	 */
	public static <T> T parseJsonToBean(String jsonString, Class<T> cls) {
		T t = null;

		if (!TextUtils.isEmpty(jsonString)) {
			try {
				t = JSON.parseObject(jsonString, cls);
			} catch (Exception e) {
				System.err.println("parse bean failed," + e.getMessage() + ",[" + jsonString + "]");
			}
		}
		return t;
	}

	/**
	 * jsonArray->javaBeanList
	 * 
	 * @param jsonString
	 * @param cls
	 * @return
	 */
	public static <T> List<T> parseJsonToListBean(String jsonString, Class<T> cls) {
		List<T> list = null;

		if (!TextUtils.isEmpty(jsonString)) {
			try {
				list = JSON.parseArray(jsonString, cls);
			} catch (Exception e) {
				System.err.println(e.getMessage());
			}
		}
		return list;
	}

}
